// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations;

public sealed partial class VariableWidthHistogramAggregation
{
	/// <summary>
	/// <para>
	/// The target number of buckets.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("buckets")]
	public int? Buckets { get; set; }

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("field")]
	public Elastic.Clients.Elasticsearch.Serverless.Field? Field { get; set; }

	/// <summary>
	/// <para>
	/// Specifies the number of individual documents that will be stored in memory on a shard before the initial bucketing algorithm is run.
	/// Defaults to <c>min(10 * shard_size, 50000)</c>.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("initial_buffer")]
	public int? InitialBuffer { get; set; }
	[JsonInclude, JsonPropertyName("script")]
	public Elastic.Clients.Elasticsearch.Serverless.Script? Script { get; set; }

	/// <summary>
	/// <para>
	/// The number of buckets that the coordinating node will request from each shard.
	/// Defaults to <c>buckets * 50</c>.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("shard_size")]
	public int? ShardSize { get; set; }

	public static implicit operator Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation(VariableWidthHistogramAggregation variableWidthHistogramAggregation) => Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation.VariableWidthHistogram(variableWidthHistogramAggregation);
}

public sealed partial class VariableWidthHistogramAggregationDescriptor<TDocument> : SerializableDescriptor<VariableWidthHistogramAggregationDescriptor<TDocument>>
{
	internal VariableWidthHistogramAggregationDescriptor(Action<VariableWidthHistogramAggregationDescriptor<TDocument>> configure) => configure.Invoke(this);

	public VariableWidthHistogramAggregationDescriptor() : base()
	{
	}

	private int? BucketsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private int? InitialBufferValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Script? ScriptValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor ScriptDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor> ScriptDescriptorAction { get; set; }
	private int? ShardSizeValue { get; set; }

	/// <summary>
	/// <para>
	/// The target number of buckets.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor<TDocument> Buckets(int? buckets)
	{
		BucketsValue = buckets;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor<TDocument> Field(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>
	/// Specifies the number of individual documents that will be stored in memory on a shard before the initial bucketing algorithm is run.
	/// Defaults to <c>min(10 * shard_size, 50000)</c>.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor<TDocument> InitialBuffer(int? initialBuffer)
	{
		InitialBufferValue = initialBuffer;
		return Self;
	}

	public VariableWidthHistogramAggregationDescriptor<TDocument> Script(Elastic.Clients.Elasticsearch.Serverless.Script? script)
	{
		ScriptDescriptor = null;
		ScriptDescriptorAction = null;
		ScriptValue = script;
		return Self;
	}

	public VariableWidthHistogramAggregationDescriptor<TDocument> Script(Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor descriptor)
	{
		ScriptValue = null;
		ScriptDescriptorAction = null;
		ScriptDescriptor = descriptor;
		return Self;
	}

	public VariableWidthHistogramAggregationDescriptor<TDocument> Script(Action<Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor> configure)
	{
		ScriptValue = null;
		ScriptDescriptor = null;
		ScriptDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The number of buckets that the coordinating node will request from each shard.
	/// Defaults to <c>buckets * 50</c>.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor<TDocument> ShardSize(int? shardSize)
	{
		ShardSizeValue = shardSize;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (BucketsValue.HasValue)
		{
			writer.WritePropertyName("buckets");
			writer.WriteNumberValue(BucketsValue.Value);
		}

		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (InitialBufferValue.HasValue)
		{
			writer.WritePropertyName("initial_buffer");
			writer.WriteNumberValue(InitialBufferValue.Value);
		}

		if (ScriptDescriptor is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, ScriptDescriptor, options);
		}
		else if (ScriptDescriptorAction is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor(ScriptDescriptorAction), options);
		}
		else if (ScriptValue is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, ScriptValue, options);
		}

		if (ShardSizeValue.HasValue)
		{
			writer.WritePropertyName("shard_size");
			writer.WriteNumberValue(ShardSizeValue.Value);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class VariableWidthHistogramAggregationDescriptor : SerializableDescriptor<VariableWidthHistogramAggregationDescriptor>
{
	internal VariableWidthHistogramAggregationDescriptor(Action<VariableWidthHistogramAggregationDescriptor> configure) => configure.Invoke(this);

	public VariableWidthHistogramAggregationDescriptor() : base()
	{
	}

	private int? BucketsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private int? InitialBufferValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Script? ScriptValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor ScriptDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor> ScriptDescriptorAction { get; set; }
	private int? ShardSizeValue { get; set; }

	/// <summary>
	/// <para>
	/// The target number of buckets.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor Buckets(int? buckets)
	{
		BucketsValue = buckets;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The name of the field.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>
	/// Specifies the number of individual documents that will be stored in memory on a shard before the initial bucketing algorithm is run.
	/// Defaults to <c>min(10 * shard_size, 50000)</c>.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor InitialBuffer(int? initialBuffer)
	{
		InitialBufferValue = initialBuffer;
		return Self;
	}

	public VariableWidthHistogramAggregationDescriptor Script(Elastic.Clients.Elasticsearch.Serverless.Script? script)
	{
		ScriptDescriptor = null;
		ScriptDescriptorAction = null;
		ScriptValue = script;
		return Self;
	}

	public VariableWidthHistogramAggregationDescriptor Script(Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor descriptor)
	{
		ScriptValue = null;
		ScriptDescriptorAction = null;
		ScriptDescriptor = descriptor;
		return Self;
	}

	public VariableWidthHistogramAggregationDescriptor Script(Action<Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor> configure)
	{
		ScriptValue = null;
		ScriptDescriptor = null;
		ScriptDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The number of buckets that the coordinating node will request from each shard.
	/// Defaults to <c>buckets * 50</c>.
	/// </para>
	/// </summary>
	public VariableWidthHistogramAggregationDescriptor ShardSize(int? shardSize)
	{
		ShardSizeValue = shardSize;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (BucketsValue.HasValue)
		{
			writer.WritePropertyName("buckets");
			writer.WriteNumberValue(BucketsValue.Value);
		}

		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (InitialBufferValue.HasValue)
		{
			writer.WritePropertyName("initial_buffer");
			writer.WriteNumberValue(InitialBufferValue.Value);
		}

		if (ScriptDescriptor is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, ScriptDescriptor, options);
		}
		else if (ScriptDescriptorAction is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Serverless.ScriptDescriptor(ScriptDescriptorAction), options);
		}
		else if (ScriptValue is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, ScriptValue, options);
		}

		if (ShardSizeValue.HasValue)
		{
			writer.WritePropertyName("shard_size");
			writer.WriteNumberValue(ShardSizeValue.Value);
		}

		writer.WriteEndObject();
	}
}